package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.model.PlayRoleBookVo;
import la.iok.hzsvn.lewin.movie.service.PlayRoleBookService;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("playRoleBooks")
public class PlayRoleBookController {
    private final PlayRoleBookService playRoleBookService;

    public PlayRoleBookController(PlayRoleBookService playRoleBookService) {
        this.playRoleBookService = playRoleBookService;
    }

    @DeleteMapping("{id}")
    public OperatorResponse delete(@RequestHeader Token token, @PathVariable Long id){
        playRoleBookService.delete(token,id);
        return OperatorResponses.success();
    }
    @GetMapping("movie/{movieId}")
    public ListDataResponse<PlayRoleBookVo> movieBooks(@RequestHeader Token token, @PathVariable Long movieId){
        return new ListDataResponse<>(playRoleBookService.movieRoleBooks(token,movieId));
    }
    /**
     * 上传角色电子书文件
     * @param token 操作用户
     * @param playRoleId 角色id
     * @param chapterId 章节id
     * @param file 文件
     * @param path 文件在剧本中的存储路径，相对剧本根目录
     * @return 操作结果
     */
    @PostMapping("playRole/{playRoleId}/chapter/{chapterId}/file")
    public SingleDataResponse<Long> uploadFile(@RequestHeader Token token, @PathVariable Long playRoleId, @PathVariable Long chapterId, MultipartFile file, String path){
        return new SingleDataResponse<>(playRoleBookService.uploadFile(token,playRoleId,chapterId,path,file));
    }

    /**
     * 查询角色文件摘要信息
     * @param id 角色id
     * @return 角色文件摘要信息
     */
    @GetMapping("{id}/digest")
    public SingleDataResponse<String> digest(@PathVariable Long id){
        return OperatorResponses.stringSingleDataResponse(playRoleBookService.digest(id));
    }

    /**
     * 查询角色文件存储路径
     * @param id 角色id
     * @return 角色文件存储路径
     */
    @GetMapping("{id}/storePath")
    public SingleDataResponse<String> storePath(@PathVariable Long id){
        return OperatorResponses.stringSingleDataResponse(playRoleBookService.storePath(id));
    }

    /**
     * 下载电子书文件
     * @param token 操作用户
     * @param response 响应
     * @param id 电子书id
     */
    @GetMapping("{id}/file")
    public void downloadFile(@RequestHeader Token token, HttpServletResponse response, @PathVariable Long id){
        playRoleBookService.downloadFile(token,response,id);
    }
}
